import scrapy
from ..items import ImgproItem
import time


class ImgSpider(scrapy.Spider):
    name = "img"
    # allowed_domains = ["www.xx.com"]
    start_urls = ["https://sc.chinaz.com/tupian/renwutupian.html"]
    url = "https://sc.chinaz.com/tupian/index_%d.html"
    pageNum = 2

    def detail_img(self, response):
        item = response.meta['item']
        img_src = response.xpath("/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/img/@src")[0].get()
        # print("detail_img_src",img_src)
        src="https:"+img_src
        item['src']=src
        print(src)
        yield item

    def parse(self, response):
        div_list = response.xpath('/html/body/div[3]/div[2]/div')
        for div in div_list:
            src = div.xpath('./div/a/@href')[0].get()
            title = div.xpath('./div/a/@title')[0].get()
            # print("src=",src)
            img_src = "https://sc.chinaz.com" + src
            # print(img_src)
            item = ImgproItem()
            item['title']=title
            # 请求传参:meta={} 可以将meta字典传递给请求对应的回调函数
            time.sleep(0.1)
            yield scrapy.Request(url=img_src, callback=self.detail_img, meta={'item': item})

        if self.pageNum <= 10:
            new_url = format(self.url % self.pageNum)
            self.pageNum += 1
            yield scrapy.Request(url=new_url, callback=self.parse)
